﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Admin_QuanLyHangHoa_ThemMoiHangHoa : System.Web.UI.Page
{
    private DataAccess da = new DataAccess();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminID"] == null)
        {
            Response.Write("<script language='javascript'>alert('Bạn phải đăng nhập / You must login');location.href='/../Admin/Login.aspx';</script>");
        }
        if (!IsPostBack)
        {
            DDL_Nhomhang.Items.Clear();
            FillDropDownList();           
        } 
    }

    private void FillDropDownList()
    {
        SqlCommand comm1 = new SqlCommand("GetAllNhomHang", da.SqlConn());
        comm1.CommandType = CommandType.StoredProcedure;
        SqlDataReader drd1;
        comm1.Connection.Open();
        drd1 = comm1.ExecuteReader();
        while (drd1.Read())
        {
            ListItem ListNhom = new ListItem();
            ListNhom.Text = "" + drd1["TenNhomHang"].ToString();
            ListNhom.Value = drd1["MaNhomHang"].ToString();
            DDL_Nhomhang.Items.Add(ListNhom);
        }
        drd1.Close();
        comm1.Connection.Close();
        comm1.Connection.Dispose();
    }
    protected bool ExistsMaHang(string strMa)
    {
        bool bOutPut = false;
        SqlDataReader drd = null;
        SqlCommand comm = new SqlCommand("isExistsMaHang", da.SqlConn());
        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.AddWithValue("@ma", strMa);
        try
        {
            comm.Connection.Open();
            drd = comm.ExecuteReader();
            if (drd.Read())
                bOutPut = true;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message.ToString());
        }
        finally
        {
            comm.Connection.Close();
            comm.Connection.Dispose();
        }
        return bOutPut;
    }
    protected void Hang_AddNew_Click(object sender, EventArgs e)
    {
        InsertHang();
    }
    protected void InsertHang()
    {
        string strMaNhom = "";
        if (DDL_Nhomhang.SelectedValue.ToString() != "")
        {
            strMaNhom = DDL_Nhomhang.SelectedValue.ToString();
        }        
        string strMa = this.txtMa.Text;
        string strTenHang = this.txtTenHang.Text;
        string strMota =  this.txtMota.Text;
        IMG_Up.SaveAs(Server.MapPath("../../Images/Images_hanghoa/") + IMG_Up.FileName);
        string strImages = "../../Images/Images_hanghoa/" + IMG_Up.FileName;
        decimal dongia = Convert.ToDecimal(this.txtDonGia.Text);


        if (ExistsMaHang(strMa) != true)
        {
            SqlCommand comm = new SqlCommand("InsertHang", da.SqlConn());
            comm.CommandType = CommandType.StoredProcedure;

            comm.Parameters.AddWithValue("@MaHang", strMa);
            comm.Parameters.AddWithValue("@MaNhom", strMaNhom);            
            comm.Parameters.AddWithValue("@TenHang", strTenHang);
            comm.Parameters.AddWithValue("@Mota", strMota);
            comm.Parameters.AddWithValue("@Images", strImages);
            comm.Parameters.AddWithValue("@DonGia", dongia);

            comm.Connection.Open();
            if (comm.ExecuteNonQuery() == -1)
            {
                Response.Redirect("DanhSachHangHoa.aspx");
            }

            comm.Connection.Close();
            comm.Connection.Dispose();
            Response.Redirect("DanhSachHangHoa.aspx");

        }
        else
        {
            HttpContext.Current.Response.Write("<script language=\"javascript\">alert('Mã hàng này đã tồn tại !');</script>");
        }

    }
}
